import pymongo
import datetime
import pymssql

def getDecimal(v):
    if(type(v).__name__ == 'Decimal128'):
        return float(str(v))
    elif(type(v).__name__ == 'datetime'):
        return v.strftime('%Y-%m-%d %H:%M:%S')
    elif (type(v).__name__ == 'bool'):
        if  v == 'False':
            v = 1
        else:
            v = 0
    return v

field = []
conn = pymssql.connect('172.16.10.30', 'sa', '1qaz@WSX', 'BMS')
cursor = conn.cursor()
sql = "select name from syscolumns where id=object_id('Operationlog')"
cursor.execute(sql)
list = cursor.fetchall()
for i in list:
    field.append(i[0])
print(field)

myclient = pymongo.MongoClient(
        "mongodb://readonly:t4vj0pshhgkwm@192.168.200.108:27017/?authSource=admin")  #
mydb = myclient["bms_pre"]
mycol = mydb['OperationLog']
now = datetime.datetime.now()
end_day = now + datetime.timedelta(days=-1)
nowstr_end = end_day.strftime('%Y-%m-%dT23:59:59')
end_day = datetime.datetime.fromisoformat(nowstr_end)
end_day = end_day.astimezone()
print(end_day)
query = {'CreationTime': {'$lte': end_day}}
mongodb_data = mycol.find(query)
s = mongodb_data.count()
print(mongodb_data)
print(s)

data = {}
for item in mongodb_data:
    print(item["CreationTime"])
    for k,v in item.items():
        if k in field:
            data[k] = getDecimal(v)
